package com.yhsimon.distributed_lock.tmp;

/**
 * @author： YHSimon
 * @date： 2022-09-04 18:36
 */
public class Test2 {
    private Lock lock=new Lock();
    public static void main(String[] args) throws InterruptedException {
        new Test2().method1();
    }

    public void method1() throws InterruptedException {
        try {
            lock.lock();
            System.out.println("method1");
            method2();
        } finally {
            lock.unlock();
        }
    }

    public void method2() throws InterruptedException {
        try {
            lock.lock();
            System.out.println("method2");
        } finally {
            lock.unlock();
        }
    }
}
class Lock{
    private boolean isLocked=false;

    public synchronized void lock() throws InterruptedException {
        while (isLocked){
            wait();
            System.out.println("wait");
        }
        isLocked=true;
    }

    public synchronized void unlock(){
        isLocked=false;
        notify();
    }
}